# !/usr/bin/env python
# -*-coding:utf-8 -*-
"""
# File       : MyThread.py
# Time       ：2023-08-12 9:27
# Author     ：张铨
# version    ：
# Description：处理线程。
"""
import datetime

from PyQt5.QtCore import QThread, pyqtSignal

from utils import StrsData
from utils.MakeIDNum import randomIDNum, randomName, create_social_credit, create_organization, randomCompanyName, \
    generate_id_number
from utils.idaddress import ID_address


class BackThread(QThread):
    '''
    设置完成信号，提供给界面
    '''
    finished = pyqtSignal()
    dataSingnal = pyqtSignal(str)
    # 多个参数时候，设置列表信号
    dataListSingal = pyqtSignal(list)

    def __init__(self):
        super(QThread,self).__init__()
        print("初始化线程")
        self.paramtype =''
        self.CNum = ''

    def getparam(self,list):
        '''
        :param list: 参数列表
        :return:
        '''
        self.paramtype = list[0]
        self.CNum = list[1]
        self.SureParamList = list[2]

    def getID(self,area_str):
        for key, value in ID_address.items():

            if area_str in value:
                area_id = key
                return area_id

    def run(self):
        '''
        线程实际处理函数
        :param self:
        :return:
        '''
        if int(self.CNum) > 1000:
            self.dataSingnal.emit("个数太多不想生成")

        else:

            if self.paramtype == "姓名":
                '''
                功能完成
                '''
                # 根据传入个数进行循环生成
                namelist = []
                for runNum in range(0,int(self.CNum)):
                    nameStr =  randomName()
                    print(nameStr)
                    namelist.append(nameStr)
                    # TODO 添加到列表中，信号传出。
                self.dataListSingal.emit(namelist)
            elif self.paramtype == "身份证（指定）":
                '''
                功能完成
                '''
                print(self.SureParamList)
                #TODO 根据参数模拟身份证信息。
                #信号发出
                personInfo = self.SureParamList
                areaData = personInfo[1]
                dayData = personInfo[2]
                sexData = personInfo[3]
                # 将指定信息转为函数参数
                provinceID = self.getID(areaData)
                birth_date = datetime.datetime.strptime(dayData, '%Y-%m-%d')  # win7
                if sexData=="男":
                    gender = 'male'
                else:
                    gender = "female"
                PersonIDList =[]
                for runNum in range(0,int(self.CNum)):
                    PersonID = generate_id_number(provinceID, birth_date, gender)
                    PersonIDList.append(PersonID)

                self.dataListSingal.emit(PersonIDList)

            elif self.paramtype == "身份证（随机）":
                '''
                功能完成
                '''
                IDlist = []
                for runNum in range(0,int(self.CNum)):
                    id = randomIDNum()
                    print(id)
                    IDlist.append(id)
                    # TODO 添加到列表中，信号传出。
                self.dataListSingal.emit(IDlist)

            elif self.paramtype == "统一社会信用代码（指定）":
                '''
                功能完成
                '''
                infoList = self.SureParamList
                areaData = infoList[1]
                provinceID = self.getID(areaData)
                idList = []
                for runNum in range(0,int(self.CNum)):
                    specialID = create_social_credit(provinceID)
                    idList.append(specialID)
                self.dataListSingal.emit(idList)

            elif self.paramtype == "统一社会信用代码（随机）":
                '''
                功能完成
                '''
                socialCodelist = []
                for runNum in range(0,int(self.CNum)):
                    social_code = create_social_credit()
                    socialCodelist.append(social_code)
                    # TODO 添加到列表中，信号传出。
                self.dataListSingal.emit(socialCodelist)

            elif self.paramtype == "组织信用代码":
                '''
                   功能完成
                '''
                organizationList = []
                for runNum in range(0, int(self.CNum)):
                    social_code = create_organization()

                    organizationList.append(social_code)
                    # TODO 添加到列表中，信号传出。
                self.dataListSingal.emit(organizationList)
            elif self.paramtype == "公司名称":
                '''
                功能完成
                '''
                companyList = []
                for runNum in range(0, int(self.CNum)):
                    companyname = randomCompanyName()
                    companyList.append(companyname)
                    # TODO 添加到列表中，信号传出。
                self.dataListSingal.emit(companyList)
            elif self.paramtype == "组合信息生成":
                '''
                功能完成
                '''
                #组合信息
                showInfoList = []
                for runNum in range(0, int(self.CNum)):
                    #名字
                    nameStr = randomName()
                    # 身份证
                    id = randomIDNum()
                    # 公司名字
                    companyname = randomCompanyName()
                    #社会统一信用代码
                    social_code = create_social_credit()
                    comstr = nameStr + "\n  身份证： " + id +"\n  公司名： " + companyname + "\n  社会统一信用代码: " + social_code
                    showInfoList.append(comstr)
                self.dataListSingal.emit(showInfoList)
            elif self.paramtype == "测试汉字字数":
                '''
                功能实现
                '''
                num = int(self.CNum)
                if num < 1000:
                    # a = a.decode("utf-8")
                    showStr = StrsData.strs[0:int(self.CNum)]
                    showStr = ''.join(showStr)
                    print(showStr)
                    self.dataSingnal.emit(showStr)
                else:
                    self.dataSingnal.emit("字数太多，不想生成")
            elif self.paramtype == "测试英文字数":
                self.dataSingnal.emit("好像没太大必要，暂时不考虑")
            elif self.paramtype == "测试特殊字符":
                self.dataSingnal.emit("好像没太大必要，暂时不考虑")


            self.finished.emit()



